//
//  TUIGuestMessageCell.m
//  MarryBackEnd
//
//  Created by mac on 2021/4/12.
//  Copyright © 2021 WPP. All rights reserved.
//

#import "TUIGuestMessageCell.h"
#import <MMLayout/UIView+MMLayout.h>
@implementation TUIGuestMessageCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.container.backgroundColor = [UIColor whiteColor];
        self.container.layer.cornerRadius = 5;
        self.container.layer.masksToBounds = YES;
        
        _topGrayContainer = [[UIView alloc]init];
        [self.container addSubview:_topGrayContainer];
        _topGrayContainer.backgroundColor = rgba(248, 249, 251, 1);
        _topGrayContainer.layer.cornerRadius = 4*ADAPTER_WIDTH;
        _topGrayContainer.layer.masksToBounds = YES;
        
        
        _firstLab = [[UILabel alloc]init];
        [_topGrayContainer addSubview:_firstLab];
        _firstLab.textAlignment = NSTextAlignmentLeft;
        _firstLab.numberOfLines = 1;
        
        _secondLab = [[UILabel alloc]init];
        [_topGrayContainer addSubview:_secondLab];
        _secondLab.textAlignment = NSTextAlignmentLeft;
        _secondLab.numberOfLines = 1;
        
        _tipLab = [[UILabel alloc]init];
        [self.container addSubview:_tipLab];
        _tipLab.textAlignment = NSTextAlignmentLeft;
        _tipLab.numberOfLines = 1;
        _tipLab.textColor = rgba(102, 102, 102, 1);
        _tipLab.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
        
        _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.container addSubview:_sureButton];
        _sureButton.backgroundColor = rgba(36, 199, 126, 1);
        [_sureButton setTitle:@"确认领取" forState:UIControlStateNormal];
        [_sureButton setTitleColor:WhiteColor forState:UIControlStateNormal];
        _sureButton.titleLabel.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
        _sureButton.layer.cornerRadius = 14*ADAPTER_WIDTH;
        _sureButton.layer.masksToBounds = YES;
        [_sureButton addTarget:self action:@selector(clickSureButton:) forControlEvents:UIControlEventTouchUpInside];
        
        _nameLab = [[UILabel alloc]init];
        [self.container addSubview:_nameLab];
        _nameLab.textAlignment = NSTextAlignmentLeft;
        _nameLab.numberOfLines = 1;
        _nameLab.textColor = rgba(51, 51, 51, 1);
        _nameLab.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
        
        _phoneLab = [[UILabel alloc]init];
        [self.container addSubview:_phoneLab];
        _phoneLab.textAlignment = NSTextAlignmentLeft;
        _phoneLab.numberOfLines = 1;
        _phoneLab.textColor = rgba(51, 51, 51, 1);
        _phoneLab.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
        
        _lineView = [[UIView alloc]init];
        [self.container addSubview:_lineView];
        _lineView.backgroundColor = rgba(206, 206, 206, 1);
        
        _gradientView = [[UIView alloc]init];
        [self.container addSubview:_gradientView];
        
        _editeButton = [BaseAdjustButton buttonWithType:UIButtonTypeCustom];
        [self.container addSubview:_editeButton];
        _editeButton.type = layoutTypeOnlyImage;
        _editeButton.imageWidth = 11*ADAPTER_WIDTH;
        _editeButton.imageHeight = 12*ADAPTER_WIDTH;
        _editeButton.marginLeft = 5*ADAPTER_WIDTH;
        _editeButton.marginRight = 10*ADAPTER_WIDTH;
        _editeButton.marginTop = 10*ADAPTER_WIDTH;
        _editeButton.marginBottom = 10*ADAPTER_WIDTH;
        [_editeButton begin];
        [_editeButton setImage:[UIImage imageNamed:@"mine_edit"] forState:UIControlStateNormal];
        [_editeButton addTarget:self action:@selector(clickEditeButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
- (void)fillWithData:(TUIGuestMessageCellData *)data
{
    [super fillWithData:data];
    self.guestCellData = data;
    self.firstLab.attributedText = data.consultingStarAttributeStr;
    self.secondLab.attributedText = data.arrivalGiftAttributeStr;
    self.tipLab.text = @"请确认您的姓名及联系方式，作为领取优惠的凭证";
    self.nameLab.text = data.customModel.ext.cdUserName;
    self.phoneLab.text = data.customModel.ext.cdUserTel;
    
    if (data.customModel.ext.isReceive == YES) {
        self.sureButton.backgroundColor = rgba(221, 221, 221, 1);
        self.sureButton.userInteractionEnabled = NO;
        self.editeButton.userInteractionEnabled = NO;
    } else {
        self.sureButton.backgroundColor = rgba(255, 66, 66, 1);
        self.sureButton.userInteractionEnabled = YES;
        self.editeButton.userInteractionEnabled = YES;
    }
    
    self.nameLabel.hidden = YES;
    self.avatarView.hidden = YES;
    self.retryView.hidden = YES;
    [self.indicator stopAnimating];
    [self setNeedsLayout];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    CGSize csize = [self.guestCellData contentSize];
    self.container.mm_left(21*ADAPTER_WIDTH).mm_width(csize.width).mm_height(csize.height);
    self.topGrayContainer.mm_left(12*ADAPTER_WIDTH).mm_top(12*ADAPTER_WIDTH).mm_width(csize.width - 24*ADAPTER_WIDTH).mm_height(70*ADAPTER_WIDTH);
    self.firstLab.frame = CGRectMake(12*ADAPTER_WIDTH, 10*ADAPTER_WIDTH, csize.width - 48*ADAPTER_WIDTH, 20*ADAPTER_WIDTH);
    self.secondLab.frame = CGRectMake(12*ADAPTER_WIDTH, 8*ADAPTER_WIDTH + self.firstLab.bottom, csize.width - 48*ADAPTER_WIDTH, 20*ADAPTER_WIDTH);
    self.tipLab.frame = CGRectMake(12*ADAPTER_WIDTH, 10*ADAPTER_WIDTH + self.topGrayContainer.bottom, csize.width - 24*ADAPTER_WIDTH, 17*ADAPTER_WIDTH);
    self.sureButton.frame = CGRectMake(csize.width - 102*ADAPTER_WIDTH, self.tipLab.bottom + 15*ADAPTER_WIDTH, 90*ADAPTER_WIDTH, 28*ADAPTER_WIDTH);
    self.nameLab.frame = CGRectMake(12*ADAPTER_WIDTH, 19*ADAPTER_WIDTH + self.tipLab.bottom, self.guestCellData.userNameWidth, 20*ADAPTER_WIDTH);
    self.phoneLab.frame = CGRectMake(20*ADAPTER_WIDTH + self.nameLab.right, 19*ADAPTER_WIDTH + self.tipLab.bottom, self.guestCellData.userPhoneWidth, 20*ADAPTER_WIDTH);
    self.lineView.frame = CGRectMake(self.nameLab.right + 10*ADAPTER_WIDTH, 23*ADAPTER_WIDTH + self.tipLab.bottom, 1, 13*ADAPTER_WIDTH);
    self.gradientView.frame = CGRectMake(self.nameLab.left, self.nameLab.bottom - 6*ADAPTER_WIDTH, self.guestCellData.gradientWidth, 6*ADAPTER_WIDTH);
    [self.container sendSubviewToBack:self.gradientView];
    self.editeButton.left = self.phoneLab.right;
    self.editeButton.centerY = self.phoneLab.centerY;
    
    if (self.gradientLayer == nil) {
        CAGradientLayer *gradientLayer = [[CAGradientLayer alloc]init];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
        gradientLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.gradientView.frame), CGRectGetHeight(self.gradientView.frame));
        gradientLayer.colors = @[(__bridge id)rgba(255, 66, 66, 0.1).CGColor,(__bridge id)rgba(255, 155, 155, 0.05).CGColor,(__bridge id)rgba(255, 255, 255, 0).CGColor];
        [self.gradientView.layer addSublayer:gradientLayer];
        self.gradientLayer = gradientLayer;
    }
}

#pragma mark - click
- (void)clickSureButton:sender
{
    if ([self.guestDelegate respondsToSelector:@selector(TUIGuestMessageCellDelegateBackCell:backType:)]) {
        [self.guestDelegate TUIGuestMessageCellDelegateBackCell:self backType:1];
    }
}
- (void)clickEditeButton:sender
{
    if ([self.guestDelegate respondsToSelector:@selector(TUIGuestMessageCellDelegateBackCell:backType:)]) {
        [self.guestDelegate TUIGuestMessageCellDelegateBackCell:self backType:0];
    }
}
@end
